Measuring the loading and deployment time of resources

In Kanzi resource profiling enables you to measure the amount of time it takes to load and deploy resources and prefabs in your Kanzi application.

In Kanzi a resource is an item you can reuse in different parts of your application. For example, a Mesh Data resource defines the geometry of a Model node and you can use the same Color Brush brush in different nodes to set the Foreground Brush or Background Brush properties. Use prefabs (prefabricated templates) to structure your application and to create consistent interfaces. Prefabs allow you to create the building blocks of your application and make the application easier to maintain.

To use resource profiling:

  1. Enable resource profiling in the application configuration. See Enabling resource profiling.
  2. Implement the logging of resource profiling data in the application code, then collect and log the resource profiling data by running your application. See Logging resource profiling data.
  3. To measure the performance of the loading and deployment of resources in your Kanzi application, analyze the resource profiling data. See Analyzing resource profiling data.

Enabling resource profiling

To start resource profiling you must enable the resource profiling category.

To enable resource profiling in the application configuration, use the ProfilingCategoryFilter setting in either:

To enable or disable resource profiling during application execution, enable or disable the corresponding profiling category:

After you enable resource profiling, you can collect resource profiling data and write the data to a file. See Logging resource profiling data.

Logging resource profiling data

To analyze the loading and deployment of resources in your Kanzi application, log the resource profiling data. Before you can log resource profiling data you must enable resource profiling. See Enabling resource profiling.

After you enable resource profiling, Kanzi profiles the loading and deployment of each resource in your application and stores the profiling data in the resource profiler. Implement the logging of the collected profiling data in your application code. Kanzi provides the ResourceProfilingPtreePopulator class which you can use to populate a Boost property tree with resource profiling data. You can then log the profiling data in JSON format using the JSON formatter available in Boost.

To log resource profiling data:

  1. In your application code include the header files that are needed to log resource profiling data.
    // Defines the standard input/output stream objects.
    #include <kanzi/core/cpp/iostream.hpp>
    // Provides classes that implement string stream input/output operations.
    #include <kanzi/core/cpp/sstream.hpp>
    // Provides the functionality for populating a Boost property tree with resource profiling data.
    #include <kanzi/core.ui/resource/resource_profiling_ptree_populator.hpp>
    // Provides the functionality for translating a Boost property tree to JSON and writing it to a file.
    #include <boost/property_tree/json_parser.hpp>
  2. Add to your application the code that outputs resource profiling data.
    For example, to log profiling data to cout in the callback onShutDown() which Kanzi calls on application exit, add this code:
        void onShutdown() KZ_OVERRIDE
        {
            // Populate the Boost property tree with resource profiling data.
            ptree resourceProfilingDataTree = ResourceProfilingPtreePopulator::populatePropertyTree(getResourceManager()->getResourceProfiler());
            // Write the property tree to string stream in JSON format.
            stringstream ss;
            boost::property_tree::json_parser::write_json(ss, resourceProfilingDataTree);
            // Boost json_writer considers "/" to be an escape character and adds "\" before each "/".
            // This affects how kzb URLs are displayed.
            // This code outputs json to cout replacing "\/" with "/".
            string str(ss.str());
            string_view strView(str);
            while (!strView.empty())
            {
                size_t backSlashPos = strView.find("\\/");
                if (backSlashPos == string_view::npos)
                {
                    cout << strView;
                    strView = string_view();
                    continue;
                }
                cout << strView.substr(0, backSlashPos);
                strView = strView.substr(backSlashPos + 1);
            }
            cout << "\n";
        }
  3. Build your application using one of the Profiling build configurations and run your application. You can see the profiling data in the console window when you exit the application.
    To build and run your application on Windows:
    1. In Visual Studio build your application using the GL_vs2015_Profiling build configuration.
    2. When you are running the Kanzi Profiling build on Windows, there is no console window. To log resource profiling data, open the command line interface on your computer and run the application so that you redirect the application output to a file where you want to store the resource profiling information:
      Application\output\Win32\GL_vs2015_Profiling\My_application.exe > MyApplicationResourceProfiling.json

Kanzi writes to the output file the resource profiling data, which contains:

...
{
    "ThreadContexts": {
        "Resource manager loader thread 1": [
            ...
        ]
        "Resource manager main thread": [
            { resource profiling context of resourceX },
            { resource profiling context of resourceY },
            ...
            { resource profiling context of resourceZ }
        ],
        "Resource manager loader thread 0": [
            ...
            { resource profiling context of resourceY },
            ...
        ],
        "Resource manager loader thread 2": [
            ...
            ...
            { resource profiling context of resourceZ },
            ...
        ]
    },
    "ResourceProfilingDataSamples": [
        ...
        { profiling data sample for resourceX },
        { profiling data sample for resourceY },
        { profiling data sample for resourceZ },
        ...
   ]
}
...

For an example of resource profiling data logged for the Scroll view example, see Analyzing resource profiling data.

Analyzing resource profiling data

After you enable resource profiling and gather the data, analyze the data collected by the resource profiler.

Resource profiling data contains:

For example, collect and analyze the resource profiling data of the Scroll view example application.

The resource profiling data contains information about:

Resource acquisition

Usually at application startup Kanzi acquires the StartupPrefab prefab template and instantiates it as a Screen node. To optimize the performance of loading resources, after acquiring the startup prefab Kanzi iterates the descendant nodes of the Screen node and collects the URLs of all resources that those nodes use. Kanzi then acquires those resources asynchronously.
For example, in the Scroll view example application the City node uses the City mesh. Because the City node is one of the descendant nodes of the Screen node, Kanzi acquires the City mesh asynchronously.

In the resource profiling data collected for the Scroll view example application, among the Resource manager main thread resource profiling contexts you can find the ResourceAcquire context of the City mesh. The AcquireAsynchronously attribute of the context is set to true, showing that Kanzi acquired the City mesh asynchronously as expected.

The parent of the ResourceAcquire context is the AcquireResourcesAsynchronously context which is a dependency context of the MainLoop context. The MainLoopCounter attribute of the MainLoop context has the value "0", meaning that Kanzi acquired the City mesh resource asynchronously during the first main loop iteration.

The AcquireResourcesAsynchronously context has dependency contexts which are related to acquiring other resources used by descendant nodes of the Screen node.

        "Resource manager main thread": [
            {
                "ContextID": "233269032",
                "ContextName": "MainLoop",
                "DependenciesDuration": "720909688",
                "MainLoopCounter": "0",
                "TimeStamp": "364",
                "DependencyContexts": [
                    {
                    ...
                    },
                    {
                        "ContextID": "200060624",
                        "ContextName": "AcquireResourcesAsynchronously",
                        "DependenciesDuration": "0",
                        "TimeStamp": "107493151",
                        "DependencyContexts": [
                            ...
                            {
                                "AcquireAsynchronously": "true",
                                "ContextID": "199287920",
                                "ContextName": "ResourceAcquire",
                                "DependenciesDuration": "0",
                                "ResourceUrl": "kzb://scroll_view/Mesh Data/City",
                                "TimeStamp": "107551499"
                            },
                            ...

Resource loading

The Kanzi loading threads load in parallel those resources that are set to be loaded asynchronously. For each asynchronously loaded resource Kanzi creates a loading task and places the task to the loading queue from which the loading threads pick up loading tasks to execute. Whenever a loading thread executes a loading task, the resource profiler creates a ResourceLoading context for that loading thread.

The ResourceLoading context of the City mesh is one of the resource loading contexts of the "Resource manager loader thread 1" thread. In a ResourceLoading context:

        "Resource manager loader thread 1": [
            ...
            {
                "ContextID": "234230448",
                "ContextName": "ResourceLoading",
                "DependenciesDuration": "0",
                "LoadedResourceID": "233255432",
                "LoadedResourceType": "Kanzi.Mesh",
                "LoadedResourceUrl": "kzb://scroll_view/Mesh Data/City",
                "ResourceLoadingDuration": "869380",
                "TimeStamp": "626864207"
            }
                ...

After the loading thread finishes loading the City mesh resource, it places the loading task of the resource to the finishing queue of the resource manager to be further processed by the Kanzi main thread. The Kanzi main thread processes the finishing queue when loading threads place completed tasks to the queue.

Resource deployment

When the Kanzi main thread processes the loading task of the City mesh resource, the resource profiler creates the FinishingQueue::processTask context. In the FinishingQueue::processTask context the Description attribute shows the task index of the loading task and the number of tasks in the finishing queue. The processing of the loading task includes resource deployment which is the last stage of resource acquisition. The ResourceDeployment context is a dependency context of the FinishingQueue::processTask context.

        "Resource manager main thread": [
            {
                "ContextID": "233269032",
                "ContextName": "MainLoop",
                "DependenciesDuration": "720909688",
                "MainLoopCounter": "0",
                "TimeStamp": "364",
                "DependencyContexts": [
                        ...
                        ]
                    },
                    ...
                    {
                        "ContextID": "235741464",
                        "ContextName": "FinishingQueue::processTask",
                        "DependenciesDuration": "52920957",
                        "Description": "Task index 0, queue size 1",
                        "TimeStamp": "647529122",
                        "DependencyContexts": [
                            {
                                "ContextID": "234146616",
                                "ContextName": "ResourceDeployment",
                                "DependenciesDuration": "47990578",
                                "DeployedResourceID": "233255432",
                                "DeployedResourceType": "Kanzi.Mesh",
                                "DeployedResourceUrl": "kzb://scroll_view/Mesh Data/City",
                                "DeploymentDuration": "4930379",
                                "TimeStamp": "647530216",
                                "DependencyContexts": [
                                    ...

The City mesh uses a 3D asset and several materials. Kanzi acquires these resources during the deployment of the City mesh. The ResourceAcquire contexts of those resources that the City mesh depends on are added to the ResourceDeployment context of the City mesh as dependency contexts.
For example, in the dependency tree of the ResourceDeployment context of the City mesh you can see that the City mesh uses the Park2 material which in turn uses the Phong material type. In the ResourceDeployment context of the City mesh the DependenciesDuration attribute shows in nanoseconds how long it took to load and deploy the resources that the City mesh depends on.

                                "ContextID": "234146616",
                                "ContextName": "ResourceDeployment",
                                "DependenciesDuration": "47990578",
                                "DeployedResourceID": "233255432",
                                "DeployedResourceType": "Kanzi.Mesh",
                                "DeployedResourceUrl": "kzb://scroll_view/Mesh Data/City",
                                "DeploymentDuration": "4930379",
                                "TimeStamp": "647530216",
                                "DependencyContexts": [
                                    {
                                        "AcquireAsynchronously": "false",
                                        "ContextID": "234125352",
                                        "ContextName": "ResourceAcquire",
                                        "DependenciesDuration": "14838173",
                                        "ResourceUrl": "kzb://scroll_view/Materials/Park2",
                                        "TimeStamp": "647530946",
                                        "DependencyContexts": [
                                            {
                                                "ContextID": "234145976",
                                                "ContextName": "ResourceLoading",
                                                "DependenciesDuration": "0",
                                                "LoadedResourceID": "235741752",
                                                "LoadedResourceType": "Kanzi.Material",
                                                "LoadedResourceUrl": "kzb://scroll_view/Materials/Park2",
                                                "ResourceLoadingDuration": "0",
                                                "TimeStamp": "647550638"
                                            },
                                            {
                                                "ContextID": "234147416",
                                                "ContextName": "ResourceDeployment",
                                                "DependenciesDuration": "14806082",
                                                "DeployedResourceID": "235741752",
                                                "DeployedResourceType": "Kanzi.Material",
                                                "DeployedResourceUrl": "kzb://scroll_view/Materials/Park2",
                                                "DeploymentDuration": "32091",
                                                "TimeStamp": "647552097",
                                                "DependencyContexts": [
                                                    {
                                                        "AcquireAsynchronously": "false",
                                                        "ContextID": "234126184",
                                                        "ContextName": "ResourceAcquire",
                                                        "DependenciesDuration": "14806082",
                                                        "ResourceUrl": "kzb://scroll_view/Material Types/Phong",
                                                        "TimeStamp": "647553191",
                                                        "DependencyContexts": [
                                                            {
                                                                "ContextID": "234146536",
                                                                "ContextName": "ResourceLoading",
                                                                "DependenciesDuration": "0",
                                                                "LoadedResourceID": "235740120",
                                                                "LoadedResourceType": "Kanzi.ShaderProgram",
                                                                "LoadedResourceUrl": "kzb://scroll_view/Material Types/Phong",
                                                                "ResourceLoadingDuration": "13129",
                                                                "TimeStamp": "647562672"
                                                            },
                                                            {
                                                                "ContextID": "234146376",
                                                                "ContextName": "ResourceDeployment",
                                                                "DependenciesDuration": "0",
                                                                "DeployedResourceID": "235740120",
                                                                "DeployedResourceType": "Kanzi.ShaderProgram",
                                                                "DeployedResourceUrl": "kzb://scroll_view/Material Types/Phong",
                                                                "DeploymentDuration": "14792953",
                                                                "TimeStamp": "647576530"
                                                            }
                                                        ]
                                                    }
                                                ]
                                            }
                                        ]
                                    },

See also

Resource profiling reference

Measuring application performance

Configuring your application

Resources

Using prefabs